/**
 * 
 */
package com.nopcommerece.seleniumtest;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.FluentWait;

/**
 * @author Pallavi
 * 
 *         initialising webdriver object and create a base class
 *
 */

public class SeleniumBase {
	public WebDriver driver;

	// initializing a browser and navigating to the url

	@Before
	public void initializeBrowser() {
		driver = new FirefoxDriver();
		driver.navigate().to("http://demo.nopcommerce.com/");
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		Assert.assertEquals("nopCommerce demo store", driver.getTitle());

	}

	// closing the browser
	@After
	public void closeBrowser() {
		driver.close();

	}

	// 1.verify user is at nopcommerce website
	// 2.verify if user has navigated to register ,
	// login,Shopingchart,WishlISTpage

	@Test
	public void verifyingHeaderLinks() {
		String[] pageTitle = { "nopCommerce demo store. Register",
				"nopCommerce demo store. Login",
				"nopCommerce demo store. Shopping Cart",
				"nopCommerce demo store. Wishlist" };
		String links[] = { "ico-register", "ico-login", "cart-label",
				"ico-wishlist" };
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		for (int i = 0; i < links.length; i++) {
			driver.findElement(By.className(links[i])).click();
			Assert.assertEquals(pageTitle[i], driver.getTitle());
			driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			driver.findElement(
					By.cssSelector("img[alt='nopCommerce demo store']"))
					.click();
			Assert.assertEquals("nopCommerce demo store", driver.getTitle());
		}
		/*
		 * driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		 * String links1= "Log in";
		 * driver.findElement(By.linkText(links1)).click();
		 * Assert.assertEquals("nopCommerce demo store. Login",
		 * driver.getTitle());
		 */
	}

	@Ignore
	@Test
	public void verifyingHeaderMenu() {

	}

	
	@Test
	public void verifyingFooterMenuInformation() {
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		jse.executeScript("scroll(0, 500);");
		String[] pageTitle = { "nopCommerce demo store. Sitemap",
				"nopCommerce demo store. Shipping & Returns",
				"nopCommerce demo store. Privacy policy",
				"nopCommerce demo store. Conditions of use",
				"nopCommerce demo store. About Us",
				"nopCommerce demo store. Contact Us" };
		String links[] = { "Sitemap", "Shipping & Returns", "Privacy Notice",
				"Conditions of Use", "About us", "Contact us" };
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		for (int i = 0; i < links.length; i++) {
			driver.findElement(By.partialLinkText(links[i])).click();
			Assert.assertEquals(pageTitle[i], driver.getTitle());
			driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			driver.findElement(
					By.cssSelector("img[alt='nopCommerce demo store']"))
					.click();
			Assert.assertEquals("nopCommerce demo store", driver.getTitle());

		}

	}

	
	@Test
	public void verifyingFooterMenuCustomerService() {
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		jse.executeScript("scroll(0, 500);");
		String[] pageTitle = { "nopCommerce demo store. Search",
				"nopCommerce demo store. News Archive",
				"nopCommerce demo store. Blog",
				"nopCommerce demo store. Recently Viewed Products",
				"nopCommerce demo store. Compare Products",
				"nopCommerce demo store. Recently Added Products" };
		String links[] = { "Search", "News", "Blog",
				"Recently viewed products", "Compare products list",
				"New products" };
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		for (int i = 0; i < links.length; i++) {
			driver.findElement(By.partialLinkText(links[i])).click();
			Assert.assertEquals(pageTitle[i], driver.getTitle());
			driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			driver.findElement(
					By.cssSelector("img[alt='nopCommerce demo store']"))
					.click();
			Assert.assertEquals("nopCommerce demo store", driver.getTitle());

		}
	}

	
	@Test
	public void verifyingFooterMenuMyAccount() {
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		jse.executeScript("scroll(0, 500);");
		String[] pageTitle = { "nopCommerce demo store. Login",
				"nopCommerce demo store. Login",
				"nopCommerce demo store. Login",
				"nopCommerce demo store. Shopping Cart",
				"nopCommerce demo store. Wishlist" };
		String links[] = { "My account", "Orders", "Addresses",
				"Shopping cart", "Wishlist" };

		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		for (int i = 0; i < links.length; i++) {
			driver.findElement(By.partialLinkText(links[i])).click();
			Assert.assertEquals(pageTitle[i], driver.getTitle());
			driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			driver.findElement(
					By.cssSelector("img[alt='nopCommerce demo store']"))
					.click();
			Assert.assertEquals("nopCommerce demo store", driver.getTitle());

		}
	}

	@Test
	public void verifyingFooterMenuFollowUs() {
		/*
		 * JavascriptExecutor jse = (JavascriptExecutor) driver;
		 * jse.executeScript("scroll(0, 500);");
		 */
		String[] pageTitle = { "NopCommerce | Facebook",
				"nopCommerce (@nopCommerce) | Twitter",
				"nopCommerce demo store: News", "nopCommerce - YouTube",
				"nopCommerce - Google+" };
		String links[] = { "Facebook", "Twitter", "RSS", "YouTube", "Google+" };

		String winHandleBefore = driver.getWindowHandle();
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		for (int i = 0; i < links.length; i++) {
			driver.findElement(By.partialLinkText(links[i])).click();
			driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			for (String winHandle : driver.getWindowHandles()) {
				driver.switchTo().window(winHandle);

			}
			Assert.assertEquals(pageTitle[i], driver.getTitle());
			if (links[i].equalsIgnoreCase("RSS")) {
				driver.navigate().back();
				Assert.assertEquals("nopCommerce demo store", driver.getTitle());
			} else {
				driver.close();
				driver.switchTo().window(winHandleBefore);
			}

			driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
			driver.findElement(
					By.cssSelector("img[alt='nopCommerce demo store']"))
					.click();
			Assert.assertEquals("nopCommerce demo store", driver.getTitle());

		}

	}

}
